package com.demo.web.controller;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class BaseController {

    public String loadResourceString(String path) {

        String LINE_SEP = System.getProperty("line.separator");
        String fileName = this.getClass().getClassLoader().getResource(path).getPath();
        String osName = System.getProperty("os.name");
        //System.out.println(osName);
        if(osName.startsWith("win") || osName.startsWith("Win")) {
            if(fileName.startsWith("/")) {
                fileName = fileName.substring(1);
            }
        }
        //System.out.println(fileName);
        try {
            List<String> lines = Files.readAllLines(Paths.get(fileName), StandardCharsets.UTF_8);
            StringBuilder sb = new StringBuilder();
            for (String s : lines) {
                sb.append(s);
                sb.append(LINE_SEP);
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}
